package com.d6game.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.d6game.web.entity.Message;
import com.d6game.web.entity.User;
import com.d6game.web.service.UserService;
import com.d6game.web.util.ResponseUtil;

@Controller
@RequestMapping(value = "/")
public class CommonController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(CommonController.class);  
	@Autowired
	UserService userService;
	
	@RequestMapping(value="/logout")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		session.removeAttribute("user");
		return new ModelAndView(new RedirectView("/"));
	}
	@RequestMapping(value="/login")
	public void login(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if(username != null && !username.equals("")){
			User user = userService.findUser(username);
			if(user != null && user.getPassword().equals(password)){
				session.setAttribute("user", user);
				Message message = new Message(1001, "");
				ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			}else if (user == null) {
				Message message = new Message(2001, "用户不存在!");
				ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			}else{
				Message message = new Message(2001, "密码错误!");
				ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			}
		}else {
			Message message = new Message(2002, "用户名为空!");
			ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
		}
	}
	@RequestMapping(value="/user/saveuser.do")
	public void saveuser(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		if(username == null || username.equals("")){
			Message message = new Message(2003, "用户名不能为空!");
			ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			return;
		}
		if(email == null || email.equals("")){
			Message message = new Message(2004, "Email不能为空!");
			ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			return;
		}
		if(password == null || password.equals("")){
			Message message = new Message(2005, "密码不能为空!");
			ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			return;
		}
		User user = userService.findUser(username);
		if(user != null){
			Message message = new Message(2006, "存在的用户名!");
			ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			return;
		}
		user = userService.findUserByEmail(email);
		if(user != null){
			Message message = new Message(2007, "存在的邮箱!");
			ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
			return;
		}
		user = new User();
		user.setEmail(email);
		user.setUsername(username);
		user.setPassword(password);
		user.setRole("1");
		userService.saveUser(user);
		logger.info(username+" "+email+" "+password);
		Message message = new Message(1001, "注册成功!");
		ResponseUtil.sendDirectResponse(response, JSONObject.fromObject(message));
	}
}
